package com.ereport.controller.plannerController;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.ereport.dao.BudgetPlannerDAO;
import com.ereport.entity.BudgetPlanner;
import com.ereport.entity.Executives;
import com.ereport.entity.OppParts;

@Controller
@RequestMapping("/budgetPlanner")
public class BudgetPlannerController {

	@RequestMapping("/open.do")
	public ModelAndView open(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		model.setViewName("/jsp/budgetPlanner.jsp");
		return model;
	}
	@RequestMapping(value="/save.do", method=RequestMethod.POST)
	public ModelAndView insert(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		String mnth=req.getParameter("month");
		String year=req.getParameter("year");
		if(mnth.length()!=0 && year.length()!=0){
		int rCount=Integer.parseInt(req.getParameter("rCount"));
		System.out.println("row count "+rCount );
		BudgetPlannerDAO bpdao=new BudgetPlannerDAO();
		try {
			bpdao.deletBudget(ex.getId(),mnth,year);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		for(int i=1;i<=rCount;i++){
			String org=req.getParameter("org"+i);
			System.out.println("organisation "+org);
			String loc=req.getParameter("loc"+i);
			System.out.println("location "+loc);
			String tndr=req.getParameter("te"+i);
			Double te=0.0;
			if(tndr!=null){
				if(tndr.length()!=0){
					te=Double.parseDouble(tndr);
				}
			}
			String cashp=req.getParameter("cp"+i);
			Double cp=0.0;
			if(cashp!=null){
				if(cashp.length()!=0){
					cp=Double.parseDouble(cashp);
				}
			}
			String fie=req.getParameter("fe"+i);
			Double fe=0.0;
			if(fie!=null){
				if(fie.length()!=0){
					fe=Double.parseDouble(fie);
				}
			}
			BudgetPlanner bp=null;
			if(org!=null && loc!=null){
			bp=new BudgetPlanner(ex,mnth,year,org,loc,te,cp,fe); 
			
			try {
				bpdao.insert(bp);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			}
		}
		
		model.setViewName("redirect:open.do");
		model.addObject("success", "Successfully Inserted");
		return model;
		}
		model.setViewName("redirect:open.do");
		return model;
	}
	@RequestMapping(value="/getbudget.do", method=RequestMethod.GET)
	public ModelAndView getBudgetPlan(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		String mnth=req.getParameter("month");
		String year=req.getParameter("year");
		BudgetPlannerDAO bpdao=new BudgetPlannerDAO();
		List<BudgetPlanner> bplist=null;
		try {
			bplist=bpdao.getBudget(ex.getId(),mnth,year);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.setViewName("/jsp/budgetPlanTable.jsp");
		model.addObject("bplist",bplist);
		return model;
		
	}
}
